package com.yaozheng.vocationaltraining.service.impl;

import android.text.TextUtils;

import com.google.gson.JsonObject;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IMeQuestionsEncyclopediaView;
import com.yaozheng.vocationaltraining.iview.IQuestionsDetailView;
import com.yaozheng.vocationaltraining.service.QuestionsDetailService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import java.io.IOException;
import java.util.HashMap;

/**
 * Created by moon on 2017/11/9.
 */

@EBean
public class QuestionsDetailServiceImpl implements QuestionsDetailService {
    private IQuestionsDetailView iTokenBaseView;
    @Override
    public void init(IQuestionsDetailView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;

    }

    @Background
    @Override
    public void list(int lastId,int questionId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get( Constants.ROOTURL + "/knowledge/question/"+questionId+"/answers?"
                            +"offset="+lastId, iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.responseSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.responseError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void loadQuestionDetail(int questionId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/knowledge/question/"+ questionId,
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.questionDetailSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.questionDetailError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .questionDetailError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void reViewQuestion(int questionId, boolean approved) {
        try {
//            JSONObject json = new JSONObject();
//            if(approved){
//                json.put("approved",approved);
//            }
//
//            OkHttpClientManager.getInstance().postJson(Constants.ROOTURL + "/knowledge/question/" + questionId,
//                    iTokenBaseView.getToken(),json.toString(), new Callback() {
//                        @Override
//                        public void onFailure(Request request, IOException e) {
//                            iTokenBaseView.reViewQuestionError("审核失败");
//                        }
//
//                        @Override
//                        public void onResponse(Response response) throws IOException {
//                            iTokenBaseView.reViewQuestionSuccess(new JSONObject());
//                        }
//                    });

            HashMap<String, String> para = new HashMap<String, String>();
//            para.put("approved", approved?"true":"false");
            if(approved){
                para.put("approved", "false");
            }else {
                para.put("aaaa", "xxx");
            }
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/knowledge/question/"+questionId,
                    iTokenBaseView.getToken(), para, new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.reViewQuestionSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.reViewQuestionError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .reViewQuestionError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void reViewAnswer(int answerId, boolean approved) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("approved", approved?"true":"false");
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/knowledge/answer/"+answerId,
                    iTokenBaseView.getToken(), para, new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.reViewAnswerSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.reViewAnswerError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .reViewAnswerError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void answerSelect(int answerId) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("aaa", "bbb");
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/knowledge/answer/"+answerId+"/select",
                    iTokenBaseView.getToken(),para, new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.answerSelectSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.answerSelectError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .answerSelectError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void setLike(final int answerId) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("aaa", "bbb");
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/knowledge/answer/"+answerId+"/like",
                    iTokenBaseView.getToken(),para, new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.setLikeSuccess(jsonObject,answerId);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.setLikeError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .setLikeError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void addAnswerReward(int answerId, String score) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("score", score);
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/knowledge/answer/"+answerId+"/add",
                    iTokenBaseView.getToken(),para, new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.addAnswerRewardSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.addAnswerRewardError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .addAnswerRewardError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
